import 'package:fa_notes/commom/database.dart';
import 'package:fa_notes/commom/type_color.dart';
import 'package:fa_notes/model/notes.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class DetailNotesPage extends StatefulWidget {
  Notes notes;
  int dropValue = 0;
  DetailNotesPage(this.notes);
  DetailNotesPage.add(this.dropValue);

  @override
  _DetailNotesPageState createState() => _DetailNotesPageState();
}

class _DetailNotesPageState extends State<DetailNotesPage> {
  String title;
  bool add;
  TextEditingController _ctrlTitle, _ctrlContent;
  var _keyScaffold = GlobalKey<ScaffoldState>();
  int typeNotes = 0;

  @override
  void initState() {
    _ctrlTitle = TextEditingController();
    _ctrlContent = TextEditingController();
    if (widget.notes == null) {
      title = "添加笔记";
      add = true;
      widget.dropValue = 0;
    } else {
      title = "笔记详情";
      add = false;
      _ctrlTitle.text = widget.notes.title;
      _ctrlContent.text = widget.notes.content;
      widget.dropValue = widget.notes.type;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _keyScaffold,
      appBar: AppBar(
        title: Text(title),
        actions: [
          FlatButton(
            child: Text(
              "保存笔记",
              style: GoogleFonts.aBeeZee(
                  color: Colors.white, fontWeight: FontWeight.bold),
            ),
            onPressed: () => _fncAction(),
          )
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: SingleChildScrollView(
          child: Column(
            children: [
              FractionallySizedBox(
                widthFactor: 1.0,
                child: DropdownButton<int>(
                  value: widget.dropValue,
                  isExpanded: true,
                  items: [
                    DropdownMenuItem(
                      value: 0,
                      child: Text(
                        "数学",
                        style: TextStyle(color: ColorType.color[0]),
                      ),
                    ),
                    DropdownMenuItem(
                      value: 1,
                      child: Text("语文",
                          style: TextStyle(color: ColorType.color[1])),
                    ),
                    DropdownMenuItem(
                      value: 2,
                      child: Text("英语",
                          style: TextStyle(color: ColorType.color[2])),
                    )
                  ],
                  onChanged: (value) {
                    widget.dropValue = value;
                    setState(() {});
                  },
                ),
              ),
              TextFormField(
                controller: _ctrlTitle,
                decoration: InputDecoration(
                    labelText: "填写笔记标题",
                    hintText: "填写笔记标题",
                    border: InputBorder.none),
                style: GoogleFonts.lato(fontWeight: FontWeight.bold),
              ),
              Divider(),
              TextFormField(
                keyboardType: TextInputType.multiline,
                maxLines: null,
                minLines: 1,
                controller: _ctrlContent,
                decoration: InputDecoration(
                    labelText: "填写笔记详情",
                    hintText: "填写笔记详情",
                    border: InputBorder.none),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _fncAction() {
    String titleNotes = _ctrlTitle.text.trim();
    String contentNotes = _ctrlContent.text.trim();
    if (titleNotes.isEmpty && contentNotes.isEmpty) {
      _keyScaffold.currentState.showSnackBar(SnackBar(content: Text("笔记为空!")));
    } else {
      if (add) {
        Notes notes = Notes(0, titleNotes, contentNotes, widget.dropValue,
            DateTime.now(), DateTime.now(), 1);
        DatabaseApp.insertNotes(notes);
        Navigator.pop(context);
      } else {
        Notes notes = Notes(widget.notes.id, titleNotes, contentNotes,
            widget.dropValue, widget.notes.dateCreated, DateTime.now(), 1);
        DatabaseApp.updateNotes(notes);
        Navigator.pop(context);
      }
    }
  }
}
